<template>
  <basic-container>
    <avue-crud
      :option="option"
      :search.sync="search"
      :table-loading="loading"
      :data="data"
      :page.sync="page"
      :permission="permissionList"
      :before-open="beforeOpen"
      v-model="form"
      ref="crud"
      @row-update="rowUpdate"
      @row-save="rowSave"
      @row-del="rowDel"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @sort-change="sortChange"
      @on-load="onLoad"
    >
      <template slot="menuLeft">
        <el-button
          type="primary"
          size="small"
          icon="el-icon-edit-outline"
          v-if="permission.reject_reason_add"
          @click="handleAddType"
          >类型配置
        </el-button>
        <el-button
          type="primary"
          size="small"
          icon="el-icon-plus"
          v-if="permission.reject_reason_add"
          @click="handleAddReason"
          >新增理由
        </el-button>
        <!-- <el-button
          type="danger"
          size="small"
          icon="el-icon-delete"
          plain
          v-if="permission.reject_reason_delete"
          @click="handleDelete"
          >删 除
        </el-button> -->
      </template>
      <!-- <template slot="menuRight">
        <el-button
          type="success"
          size="small"
          plain
          icon="el-icon-upload2"
          v-if="permission.reject_reason_import"
          @click="handleImport"
          >导入
        </el-button>
        <el-button
          type="warning"
          size="small"
          plain
          icon="el-icon-download"
          v-if="permission.reject_reason_export"
          @click="handleExport"
          >导出
        </el-button>
      </template> -->
    </avue-crud>
    <el-dialog
      title="类型配置"
      append-to-body
      :close-on-click-modal="false"
      :visible.sync="dialogTypeVisible"
    >
      <template slot="" slot-scope="" v-if="dialogTypeVisible">
        <avue-crud
          :option="infoTypeOption"
          :data="infoTypeData"
          :search.sync="infoTypeSearch"
          v-model="infoTypeForm"
          :table-loading="infoTypeloading"
          ref="InfoTypeCrud"
          :page.sync="pageInfoType"
          @search-change="searchInfoTypeChange"
          @row-update="rowInfoTypeUpdate"
          @row-save="rowInfoTypeSave"
          @row-del="rowInfoTypeDel"
          @refresh-change="refreshInfoTypeChange"
          @on-load="onLoadInfoType"
         >
        </avue-crud>
      </template>
    </el-dialog>
    <el-dialog
      title="新增理由"
      append-to-body
      :close-on-click-modal="false"
      :visible.sync="dialogReasonVisible"
    >
      <avue-form ref="reasonForm" :option="reasonOption" v-model="reasonForm" @submit="reasonSubmit"></avue-form>
    </el-dialog>
    <el-dialog
      title="数据导入"
      append-to-body
      :visible.sync="excelBox"
      width="555px"
    >
      <avue-form
        :option="excelOption"
        v-model="excelForm"
        :upload-after="uploadAfter"
      >
        <template slot="excelTemplate">
          <el-button type="primary" @click="handleTemplate">
            点击下载<i class="el-icon-download el-icon--right"></i>
          </el-button>
        </template>
      </avue-form>
    </el-dialog>
  </basic-container>
</template>

<script>
import {
  getDetailSecond,
  getPageFirst,
  getPageSecond,
  submitFirst,
  submitSecond,
  deleteFirst,
  deleteSecond,
} from "@/api/rb-oem/rejectreason/reject_reason";
import { mapGetters } from "vuex";
import { getToken } from "@/util/auth";
export default {
  data() {
    return {
      form: {},
      query: {},
      search: {},
      loading: true,
      infoTypeloading: true,
      excelBox: false,
      dialogTypeVisible: false,
      dialogReasonVisible: false,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      pageInfoType: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      selectionList: [],
      option: {
        height: "calc(50vh - 50px)",
        calcHeight: 30,
        tip: false,
        border: true,
        index: true,
        viewBtn: true,
        addBtn: false,
        selection: true,
        searchMenuSpan: 6,
        columnBtn: false,
        column: [
          {
            label: "主键",
            prop: "id",
            type: "input",
            sortable: true,
            display: false,
            hide: true
          },
          {
            label: "类型ID",
            prop: "parId",
            type: "input",
            sortable: true,
            display: false,
            hide: true,
            overHidden: true,
          },
          {
            label: "驳回类型",
            prop: "parName",
            type: "input",
            search: true,
            span: 24,
            editDisabled: true,
            rules: [
              {
                required: true,
                message: "请输入驳回类型",
                trigger: "blur",
              },
            ],
            overHidden: true,
          },
          {
            label: "驳回理由",
            prop: "dataItem",
            type: "input",
            search: true,
            span: 24,
            rules: [
              {
                required: true,
                message: "请输入驳回理由",
                trigger: "blur",
              },
            ],
            overHidden: true,
          },
          {
            label: "处理方式ID",
            prop: "sonId",
            type: "input",
            display: false,
            hide: true,
            overHidden: true,
          },
          {
            label: "处理方式",
            prop: "sonName",
            type: "textarea",
            span: 24,
            rules: [
              {
                required: true,
                message: "请输入处理方式",
                trigger: "blur",
              },
            ],
            overHidden: true,
          },
          
        ],
      },
      data: [],
      infoTypeSearch: {},
      infoTypeForm: {},
      infoTypeData: [],
      infoTypeOption: {
        height: "auto",
        calcHeight: '100%',
        border: true,
        index: true,
        tip: false,
        columnBtn: false,
        searchMenuSpan: 6,
        dialogWidth: '40%',
        enter: true,
        column: [
          {
            label: "Id",
            prop: "id",
            hide: true,
            display: false
          },
          {
            label: "驳回类型",
            prop: "dataItem",
            type: "input",
            span: 24,
            search: true,
            searchSpan: 12,
            rules: [
              {
                required: true,
                message: "请输入驳回类型",
                trigger: "blur",
              },
            ],
          },
        ],
      },
      reasonForm: {},
      reasonOption: {
        column: [
          {
            label: "主键",
            prop: "id",
            type: "input",
            display: false,
            sortable: true,
          },
          // {
          //   label: "驳回类型ID",
          //   prop: "parName",
          //   type: "input",
          //   display: false,
          //   sortable: true,
          //   rules: [
          //     {
          //       required: true,
          //       message: "请输入上级id",
          //       trigger: "blur",
          //     },
          //   ],
          // },
          {
            label: "驳回类型",
            prop: "parId",
            span: 24,
            minRows: 2,
            type: "select",
            // dicUrl: "/api/rb-oem/oem/rejectReason/reject_reason/page?current=-1&size=-1",
            filterable: true,
            props: {
              label: "dataItem",
              value: "id"
            },
            dicData: [],
            rules: [
              {
                required: true,
                message: "请输入驳回类型",
                trigger: "blur",
              },
            ],
          },
          {
            label: "驳回理由",
            prop: "dataItem",
            span: 24,
            minRows: 2,
            type: "input",
            hide: true,
            rules: [
              {
                required: true,
                message: "请输入驳回理由",
                trigger: "blur",
              },
            ],
          },
          {
            label: "处理方式ID",
            prop: "sonId",
            type: "input",
            sortable: true,
            display: false,
            rules: [
              {
                required: true,
                message: "请输入上级id",
                trigger: "blur",
              },
            ],
          },
          {
            label: "处理方式",
            prop: "sonName",
            span: 24,
            minRows: 2,
            type: "textarea",
            hide: true,
            rules: [
              {
                required: true,
                message: "请输入处理方式",
                trigger: "blur",
              },
            ],
          },
        ],
      },
      excelForm: {},
      excelOption: {
        submitBtn: false,
        emptyBtn: false,
        column: [
          {
            label: "模板上传",
            prop: "excelFile",
            type: "upload",
            drag: true,
            loadText: "模板上传中，请稍等",
            span: 24,
            propsHttp: {
              res: "data",
            },
            tip: "请上传 .xls,.xlsx 标准格式文件",
            action: "/api/rb-core/core/rejectreason/reject_reason/import",
          },
          {
            label: "模板下载",
            prop: "excelTemplate",
            formslot: true,
            span: 24,
          },
        ],
      },
    };
  },
  computed: {
    ...mapGetters(["permission"]),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.reject_reason_add, false),
        viewBtn: this.vaildData(this.permission.reject_reason_view, false),
        delBtn: this.vaildData(this.permission.reject_reason_delete, false),
        editBtn: this.vaildData(this.permission.reject_reason_edit, false),
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id);
      });
      return ids.join(",");
    },
  },
  methods: {
    /* 字典 驳回类型列表 */
    getDicdata() {
      getPageFirst(
        '-1', '-1',
      ).then((res) => {
        const column = this.findObject(this.reasonOption.column, 'parId');
        column.dicData = res.data.data.records;
      });
    },
    /* dialog 驳回类型列表 */
    handleAddType() {
      this.dialogTypeVisible = true;
    },
    /* dialog 新增驳回理由 */
    handleAddReason() {
      this.dialogReasonVisible = true;
      this.getDicdata();
    },
    /* 驳回三部曲(二级/理由) 新增 */
    reasonSubmit(row, done, loading) {
      submitSecond(row).then(
        () => {
          this.onLoad(this.page);
          this.dialogReasonVisible = false;
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          this.$refs.reasonForm.resetForm();
          done();
        },
        (error) => {
          loading();
          window.console.log(error);
        }
      );
    },
    /* 驳回三部曲(二级/理由) 新增 */
    // rowSave(row, done, loading) {
    //   add(row).then(
    //     () => {
    //       this.onLoad(this.page);
    //       this.$message({
    //         type: "success",
    //         message: "操作成功!",
    //       });
    //       done();
    //     },
    //     (error) => {
    //       loading();
    //       window.console.log(error);
    //     }
    //   );
    // },
    /* 驳回三部曲(二级/理由) 删除 */
    rowUpdate(row, index, done, loading) {
      submitSecond(row).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          done();
        },
        (error) => {
          loading();
          window.console.log(error);
        }
      );
    },
    /* 驳回三部曲(二级/理由) 删除 */
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return deleteSecond(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
        });
    },
    /* 驳回类型 新增 */
    rowInfoTypeSave(row, done, loading) {
      submitFirst(row).then(
        () => {
          this.onLoadInfoType(this.pageInfoType);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          done();
        },
        (error) => {
          loading();
          window.console.log(error);
        }
      );
    },
    /* 驳回类型 修改 */
    rowInfoTypeUpdate(row, index, done, loading) {
      submitFirst(row).then(
        () => {
          this.onLoadInfoType(this.pageInfoType);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          done();
        },
        (error) => {
          loading();
          window.console.log(error);
        }
      );
    },
    /* 驳回类型 删除 */
    rowInfoTypeDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return deleteFirst(row.id);
        })
        .then(() => {
          this.onLoadInfoType(this.pageInfoType);
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
        });
    },
    /* 驳回三部曲 ids 删除 */
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return deleteSecond(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          this.$refs.crud.toggleSelection();
        });
    },
    beforeOpen(done, type) {
      if (["edit", "view"].includes(type)) {
        getDetailSecond(this.form.id).then((res) => {
          this.form = res.data.data;
        });
      }
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    /* 驳回类型 搜索 */
    searchInfoTypeChange(params, done) {
      this.infoTypeSearch = params;
      this.pageInfoType.currentPage = 1;
      this.onLoadInfoType(this.pageInfoType, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    /* 驳回三部曲 刷新 */
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    /* 驳回类型 刷新 */
    refreshInfoTypeChange() {
      this.onLoadInfoType(this.pageInfoType, this.infoTypeSearch);
    },
    /* 驳回三部曲 分页 */
    onLoad(page, params = {}) {
      this.loading = true;
      getPageSecond(
        page.currentPage,
        page.pageSize,
        Object.assign(params, this.query)
      ).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    },
    /* 驳回类型 分页 */
    onLoadInfoType(page, params = {}) {
      this.infoTypeloading = true;
      getPageFirst(
        page.currentPage,
        page.pageSize,
        Object.assign(params, this.infoTypeSearch)
      ).then((res) => {
        const data = res.data.data;
        this.pageInfoType.total = data.total;
        this.infoTypeData = data.records;
        this.infoTypeloading = false;
      });
    },
    handleImport() {
      this.excelBox = true;
    },
    handleExport() {
      let exQuery = ``;
      exQuery += `&dataType=${this.search.dataType}`;
      exQuery += `&parId=${this.search.parId}`;
      exQuery += `&dataItem=${this.search.dataItem}`;
      this.$confirm("是否导出用户数据?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        window.open(
          `/api/rb-core/core/rejectreason/reject_reason/export?${
            this.website.tokenHeader
          }=${getToken()}` + exQuery
        );
      });
    },
    handleTemplate() {
      window.open(
        `/api/rb-core/core/rejectreason/reject_reason/export-template?${
          this.website.tokenHeader
        }=${getToken()}`
      );
    },
    uploadAfter(res, done) {
      this.excelBox = false;
      this.refreshChange();
      done();
    },
    sortChange(val) {
      if (val.order == "ascending") {
        //正序
        this.query.ascs = val.prop;
        this.query.descs = "";
      } else {
        //倒序
        this.query.descs = val.prop;
        this.query.ascs = "";
      }
      this.refreshChange();
    },
  },
};
</script>

<style>
</style>
